/**
 * Copyright (c) Tapas Mobile.  All Rights Reserved.
 *
 * @author DaiHui 
 * @version 1.0
 */

package com.dotools.base;

import android.app.Application;
import android.content.res.Configuration;

import com.dotools.debug.DebugTools;
import com.dotools.debug.LOG;
import com.dotools.debug.StartupTracker;
import com.dotools.preferences.AppPreferences;
import com.dotools.preferences.BigPreferences;
import com.dotools.thread.ThreadPool;
import com.dotools.utils.DevicesUtils;
import com.dotools.utils.ProcessUtils;
import com.dotools.utils.SDCardUtils;
import com.dotools.utils.ThreadUtils;
import com.dotools.utils.UiUtils;
import com.dotools.utils.Utilities;

public class CommonLibrary {

    public static Application sApplication;

    public static void onApplicationCreate(CommonLibraryParameter parameter) {
        ThreadUtils.ensureUiThread();

        // ========== BEGIN CRITICAL STARTUP ==========
        sApplication = parameter.app;
        LOG.CURRENT_STATE = parameter.enableLog ? LOG.ALL : LOG.ERROR;
        LOG.logTag = parameter.logTag;
        boolean profileStartup = parameter.profileStartup;

        Utilities.initEnvironment(parameter.app, parameter.appName, parameter.masterProcessName);

        CommonConstants.CPU_CORES = Runtime.getRuntime().availableProcessors();
        CommonConstants.PROCESS_NAME = ProcessUtils.getProcessName();

        ThreadPool.startup();
        LOG.fileLogEnabled(parameter.enableFileLog, true); // blocked initialization
        LOG.setupUncaughtExceptionHandler();

        AppPreferences.init();
        BigPreferences.init();

        if (CommonConstants.IS_DEBUG) {
            DebugTools.enableStrictMode(); // enable strict mode after file log enabled
            // to debug memory usage aggressively
            DebugTools.clearPreloadedDrawables();
        }

        // ========== END CRITICAL STARTUP ==========

        if (profileStartup) {
            final int delay = parameter.profileDelay;

            // to debug startup aggressively
            if (SDCardUtils.sdcardWriteable() && parameter.masterProcessName.equals(ProcessUtils.getProcessName())) {
                android.os.Debug.startMethodTracing(ProcessUtils.getSafeProcessName(), parameter.profileBufferSize);
                ThreadPool.postOnWorkerDelayed(new Runnable() {
                    @Override
                    public void run() {
                        android.os.Debug.stopMethodTracing();
                    }
                }, delay);
            }
        }

        // start normal library initialization
        if (profileStartup) StartupTracker.record("Common Library starting.");

        // 0. DVM memory policy
        Utilities.setVmMemoryPolicy();

        // 1. register system event listeners

        // 2. ObjectFactory
        //ObjectFactory.createInstance();

        // 3. ObjectStore
        //ObjectStore.createInstance();

        // 4. runBackgroundInitializer
        Utilities.runBackgroundInitializer();

        LOG.logI("application [" + parameter.appName + "] created");

        if (profileStartup) StartupTracker.record("Common Library started.");
        
        if(CommonConstants.IS_DEBUG) {
            LOG.logI("DeviceDescription:"+DevicesUtils.getDeviceDescription());
        }
        PathConstance.initPath();
    }
    

    public static void onApplicationDestroy() {
        ThreadPool.shutdown(); // ThreadPool should be shutdown at last
    }

    public static void onConfigurationChange(Configuration config) {
        LOG.logI(config.toString());

        UiUtils.onConfigurationChanged(config);
    }
}
